package com.paic.qhcs.util;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.paic.qhcs.util.entry.Header;
import com.paic.qhcs.util.entry.MessageEntry;
import com.paic.qhcs.util.entry.ResultEntry;
import com.paic.qhcs.util.entry.SecurityInfo;

/**
 * 信用评分信息
 * Copyright 2015 Shudao Info Tech Co. Ltd. All rights reserved.
 * @author XSunny
 * @date 2015年10月12日 上午10:28:15
 * @version V1.0
 * 
 *
 */
public class CreditService {

	
	public static void main(String [] args){
		try {
			ResultEntry entry = queryCreditInfo("李晓玲", "433126197810055526", "13508430025", "6282880009712499");
			System.out.println(entry);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
     * 查询个人信用信息
     * @param name
     * @param idNo
     * @param mobile
     * @param cardNo
     * @return
     * @throws Exception
     */
    public static ResultEntry queryCreditInfo(String name, String idNo, String mobile, String cardNo) throws Exception{
    	Header header = new Header();
    	//提供一个批次号 日期可选
    	header.setTransNo("tranNo"+System.currentTimeMillis()%100000);
    	header.setTransDate(null); 	
    	header.setAuthDate(null);
    	
    	header.setAuthCode(Constants.AuthCode);
    	header.setChnlId(Constants.ChnlId);
    	header.setOrgCode(Constants.OrgCode);
    	
    	//填写message字段
    	List<MessageEntry> messages = new ArrayList<MessageEntry>();
    	MessageEntry message = new MessageEntry();
    	message.setReasonNo(Constants.ReasonNo);
    	message.setIdType(Constants.IdType);
    	
    	message.setIdNo(idNo);
    	message.setMobileNo(mobile);
    	message.setCardNo(cardNo);
    	message.setName(name);
    	
    	messages.add(message);
    	
    	//固定信息
    	SecurityInfo sInfo = new SecurityInfo();    	
    	sInfo.setUserName(Constants.UserName);
    	sInfo.setUserPassword(Constants.UserPassword);
    	
    	return sendRequestHttps(header, messages, "trans"+System.currentTimeMillis()%1000000, sInfo, Constants.TransformKey);
    }
    
    
    /**
     * @param header        请求头信息
     * @param message       消息实体
     * @param securityInfo  认证信息
     * @return ResultEntry  结果实体
     * @throws Exception 
     */
    public static ResultEntry sendRequestHttps(Header head, List<MessageEntry> messages, String batchNo, SecurityInfo security, String keyStr) throws Exception{
    	String sfUrl = "https://qhzx-dcs.pingan.com.cn/do/dmz/query/credoo/v1/MSC8005";
        String header = "\"header\":" + MessageUtil.getMHeader_DMZ(head); //input header here
        String encBusiData = DataSecurityUtil.encrypt(MessageUtil.getBusiData(batchNo, messages).getBytes(),
        		keyStr); //input keyStr here
        String busiData = "\"busiData\":\"" + encBusiData + "\"";
        security.setSignatureValue(DataSecurityUtil.signData(encBusiData));
        String pwd = DataSecurityUtil.digest(security.getUserPassword().getBytes());//input security here
        security.setUserPassword(pwd);
        String securityInfo = "\"securityInfo\":" + MessageUtil.getSecurityInfo(security);//input security here
        String message = "{" + header + "," + busiData + "," + securityInfo + "}";

        String res = HttpRequestUtil.sendJsonWithHttps(sfUrl, message);

        JSONObject msgJSON = net.sf.json.JSONObject.fromObject(res);
        //System.out.println("响应BusiData密文：" + msgJSON.getString("busiData"));

        try{
        // 一定要验证签名的合法性！！！！！！！！
        DataSecurityUtil.verifyData(msgJSON.getString("busiData"), msgJSON.getJSONObject("securityInfo").getString(
                "signatureValue"));
        }catch(Exception e){
        	e.printStackTrace();
        }

//        System.out.println("响应BusiData明文："
//                + DataSecurityUtil.decrypt(msgJSON.getString("busiData"), keyStr));
        JSONObject resJSON = net.sf.json.JSONObject.fromObject(DataSecurityUtil.decrypt(msgJSON.getString("busiData"), keyStr));
        JSONArray recodes = (JSONArray) resJSON.get("records");
        ResultEntry result = new ResultEntry((JSONObject) recodes.get(0));

		return result;
    }

}
